package com.example.wishstar.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wishstar.MainActivity;
import com.example.wishstar.R;
import com.example.wishstar.databinding.ActivityAddWishBinding;
import com.example.wishstar.model.CommonDate;
import com.example.wishstar.viewmodel.AddWishViewModel;

public class AddWishActivity extends AppCompatActivity implements AddWishViewModel.ChoiceWishIconCallBack{

    private static final int ICON_NUM = 16;
    private String TAG = "AddWishActivity";
    private ActivityAddWishBinding activityAddWishBinding;
    private AddWishViewModel addWishViewModel;

    private GradientDrawable iconBackground[] = new GradientDrawable[ICON_NUM];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityAddWishBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_wish);
        addWishViewModel = new AddWishViewModel(this);
        activityAddWishBinding.setAddWishViewModel(addWishViewModel);

        addWishViewModel.setChoiceWishIconCallBack(this);

        TextView title = findViewById(R.id.post_title);
        title.setText(getString(R.string.wish_post_title));

        for (int i = 0; i < ICON_NUM; ++i) {
            ImageView imageView = findByPostion(i + 1);
            iconBackground[i] = (GradientDrawable) imageView.getBackground();
            iconBackground[i].setColor(getColor(R.color.green_transparent));
        }

        ImageView backImage = findViewById(R.id.image_title_back);
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddWishActivity.this, MainActivity.class);
                startActivity(intent);
                clearIconColor();
                finish();
            }
        });

        Button button = findViewById(R.id.button_title_post);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addWishViewModel.post();
                //Toast.makeText(AddWishActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                clearIconColor();
                finish();
            }
        });

        addWishViewModel.getDate().observe(this, new Observer<CommonDate>() {
            @Override
            public void onChanged(CommonDate commonDate) {
                activityAddWishBinding.setDate(commonDate);
            }
        });
    }



    @Override
    public void callback(int wishIcon) {
        for(int i = 0; i < ICON_NUM; i++) {
            if (i + 1 == wishIcon) {
                iconBackground[i].setColor(getColor(R.color.yellow_light));
            }
            else {
                iconBackground[i].setColor(getColor(R.color.green_transparent));
            }
        }
    }

    private ImageView findByPostion(int postion) {
        switch (postion) {
            case 1:
                return findViewById(R.id.add_wish_image_icon1);
            case 2:
                return findViewById(R.id.add_wish_image_icon2);
            case 3:
                return findViewById(R.id.add_wish_image_icon3);
            case 4:
                return findViewById(R.id.add_wish_image_icon4);
            case 5:
                return findViewById(R.id.add_wish_image_icon5);
            case 6:
                return findViewById(R.id.add_wish_image_icon6);
            case 7:
                return findViewById(R.id.add_wish_image_icon7);
            case 8:
                return findViewById(R.id.add_wish_image_icon8);
            case 9:
                return findViewById(R.id.add_wish_image_icon9);
            case 10:
                return findViewById(R.id.add_wish_image_icon10);
            case 11:
                return findViewById(R.id.add_wish_image_icon11);
            case 12:
                return findViewById(R.id.add_wish_image_icon12);
            case 13:
                return findViewById(R.id.add_wish_image_icon13);
            case 14:
                return findViewById(R.id.add_wish_image_icon14);
            case 15:
                return findViewById(R.id.add_wish_image_icon15);
            case 16:
                return findViewById(R.id.add_wish_image_icon16);
        }
        return findViewById(R.id.add_wish_image_icon16);
    }

    private void clearIconColor() {
        for (int i = 0; i < ICON_NUM; ++i) {
            iconBackground[i].setColor(getColor(R.color.green_transparent));
        }
    }
}